package org.springblade.modules.material.strategy.impl;

import org.springblade.common.enums.library.QuestionType;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.modules.library.vo.QuestionOptionVO;
import org.springblade.modules.material.entity.PaperAnswerOptionEntity;
import org.springblade.modules.material.strategy.CalculateScoreStrategy;
import org.springblade.modules.material.vo.PaperReleaseDetailVO;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: junxin
 * @Description: 计算多选题
 */
public class CalculateMultipleChoice implements CalculateScoreStrategy {

	@Override
	public Integer calculateScore(List<PaperReleaseDetailVO> paperReleaseDetailVOList,
								  List<PaperAnswerOptionEntity> paperAnswerOptionEntityList, Long paperAnswerId, Integer multipleScoreSetting) {
		int score=0;
		for (PaperReleaseDetailVO paperReleaseDetailVO : paperReleaseDetailVOList) {
			//flag=1 该题分数全得，flag=0 得一半分，flag=-1 该题不得分
			int flag = 1;
			List<PaperAnswerOptionEntity> userAnswerOptionList = new ArrayList<>();
			//选项内容
			List<QuestionOptionVO> questionOptionList = paperReleaseDetailVO.getQuestionOptionList();
			for (QuestionOptionVO questionOptionVO : questionOptionList) {
				PaperAnswerOptionEntity paperAnswerOptionEntity = new PaperAnswerOptionEntity();
				paperAnswerOptionEntity.setAnswerId(paperAnswerId);
				paperAnswerOptionEntity.setUserId(AuthUtil.getUserId());
				paperAnswerOptionEntity.setChecked(questionOptionVO.getId());
				paperAnswerOptionEntity.setReleaseDetailId(paperReleaseDetailVO.getId());
				paperAnswerOptionEntity.setValue(0);
				paperAnswerOptionEntity.setQuestionId(questionOptionVO.getQuestionBankId());
				paperAnswerOptionEntity.setQuestionType(QuestionType.MULTIPLE.getCode());

				if (questionOptionVO.getChoice() == 0 && questionOptionVO.getIsRight() == 1 && flag>0){//如果是正确答案,但是没有选择，且flag>1的情况下
					flag = 0;
				}

				if (questionOptionVO.getChoice() == 1 && questionOptionVO.getIsRight() == 0) { //如果选中了错误答案，则不得分
					flag = -1;
				}
				if (questionOptionVO.getChoice() == 1)userAnswerOptionList.add(paperAnswerOptionEntity);

			}
			if (flag >= 0 ) {
				int questionScore=paperReleaseDetailVO.getValue();
				if ( flag==1||multipleScoreSetting>0){ //如果该题得全分或者 少选可得一半分
					if(flag==0)questionScore=questionScore/2;
					int finalQuestionScore = questionScore;
					userAnswerOptionList.forEach(userAnswerOption-> userAnswerOption.setValue(finalQuestionScore) );
					score+=finalQuestionScore;
				}
			}
			paperAnswerOptionEntityList.addAll(userAnswerOptionList);//保存用户的作答选项
		}
		return score;
	}

}
